﻿using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace JQMergeFramework
{
    public class UIClearingTips : MonoBehaviour
    {
        public Text cityTips;
        public Text gameNum;


        public Transform topTrans;
        public Transform bottomTrans;
        
        private Vector2 topTransDefaultPos, bottomTransDefaultPos;


        private void Awake()
        {
            topTransDefaultPos = topTrans.localPosition;
            bottomTransDefaultPos = bottomTrans.localPosition;
        }

        private void Start()
        {
            UIManager.Instance.AddBtnListener(this.transform);
        }

        
        
                
        void OnBtnClick(Transform trans)
        {
            AudioManager.Instance.Play(AudioName.Click);
            switch (trans.name)
            {
                case "BtnClose":
                    Close();
                    break;
                case "BtnShare":
                    Close();
                    break;
            }
        }


        public void Open()
        {
            topTrans.localPosition += new Vector3(0, 500, 0);
            bottomTrans.localPosition -= new Vector3(0, 500, 0);
            
            gameNum.text =   MergeManager.Instance.lastLayerNum.ToString();
            
            CityTargetCfg cfg = TableConfig.Instance.GetFinishedTargetCfg(MergeManager.Instance.lastLayerNum);

            if (cfg!= null)
            {
                cityTips.text = string.Format("相当于一个<color=#11fca6>{0}</color>的高度！",cfg.name);
            }
            else
            {
                cityTips.text = "楼层太低，什么也没有达成！";
            }

            topTrans.DOLocalMoveY(topTransDefaultPos.y, GameCfg.UIDTTime).SetEase(Ease.Linear);
            bottomTrans.DOLocalMoveY(bottomTransDefaultPos.y, GameCfg.UIDTTime).SetEase(Ease.Linear);
        }

        void Close()
        {
      
            
            topTrans.DOLocalMoveY(topTransDefaultPos.y+500, GameCfg.UIDTTime).SetEase(Ease.Linear);
            bottomTrans.DOLocalMoveY(bottomTransDefaultPos.y-500, GameCfg.UIDTTime).SetEase(Ease.Linear);
            
            CommonFun.DelayCallback(() =>
            {   
                gameObject.SetActive(false);
                DOTween.KillAll();
                SceneManager.LoadScene("Loading");
            },GameCfg.UIDTTime+0.2F).Forget();
      
        }

    }
}